﻿using System;
using System.Windows.Forms;
using DesignerHelper.Help;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Forms;

namespace WorkbookDesigner.UI
{
    public partial class NewLocateRangeDialog : Form, INameListView
    {
        private NewLocateRangeDialog()
        {
            InitializeComponent();
        }

        public NewLocateRangeDialog(WorkbookView wbv) : this()
        {
            ActiveView = wbv;
            if (ActiveView == null)
            {
                MesBox.ErrorMessage("打开窗口出错,请重试");
                return;
            }
            BookWorkOn = ActiveView.ActiveWorkbook;
            Presenter = new LocateRangePresenter(this);
        }

        private WorkbookView ActiveView { get; set; }

        private IWorkbook BookWorkOn { get; set; }

        public string NameToSearch => textBoxSearch.Text;

        public bool InSearchMode => !string.IsNullOrEmpty(NameToSearch);


        void INameListView.ResetListViewSize(int size)
        {
            listViewNames.VirtualListSize = size;
        }


        public void AutoSizeColumn()
        {
            columnHeaderNames.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            columnHeaderAddress.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        #region Interface
        LocateRangePresenter INameListView.Presenter { get; set; }
        private LocateRangePresenter Presenter
        {
            get { return ((INameListView)this).Presenter; }
            set { ((INameListView)this).Presenter = value; }
        }

        WorkbookView INameListView.ActiveView => ActiveView;

        bool INameListView.InSearchMode => !string.IsNullOrEmpty(textBoxSearch.Text);

        string INameListView.NameToSearch => textBoxSearch.Text;

        #endregion






        #region 响应窗体事件
        private void LocateRangeDialog_Load(object sender, EventArgs e)
        {
            HookEvents();
            Presenter.TriggerInit();
            AutoSizeColumn();
            Text = Presenter.FormText;
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            //TriggerInit();
            Presenter.TriggerInit();
            AutoSizeColumn();
        }
        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {

            Presenter.TriggerInit();
            AutoSizeColumn();
        }
        private void listViewNames_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            Presenter.ActivateRange(e.Item);
        }
        private void listViewNames_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
        {

        }
        private void LocateRangeDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnhookEvents();
        }


        private void listViewNames_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            e.Item = Presenter.NameItemList[e.ItemIndex];
        }

        private void HookEvents()
        {
            ActiveView.CellBeginEdit += DisableDialog;
            ActiveView.CellCancelEdit += EnableDialog;
            ActiveView.CellEndEdit += EnableDialog;
        }

        private void UnhookEvents()
        {
            ActiveView.CellBeginEdit -= DisableDialog;
            ActiveView.CellCancelEdit -= EnableDialog;
            ActiveView.CellEndEdit -= EnableDialog;

        }
        private void EnableDialog(object sender, EventArgs e)
        {
            textBoxSearch.Enabled = 
                listViewNames.Enabled =
                buttonReset.Enabled = true;
            Presenter.TriggerInit();
            AutoSizeColumn();

        }

        private void DisableDialog(object sender, EventArgs e)
        {
            textBoxSearch.Enabled =
                buttonReset.Enabled =
                listViewNames.Enabled = false;

            ((INameListView)this).ResetListViewSize(0);

        }
        private void listViewNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActiveView.FindForm()?.Activate();
        }

        void INameListView.AutoSizeColumn()
        {
            throw new NotImplementedException();
        }

        #endregion

    }


}


